# -*- coding: utf-8 -*-
# Author   : ZhangQing
# Time     : 2025-07-08 6:52
# File     : setup.py
# Project  : risk-contagion-analysis
# Desc     :

from setuptools import setup, find_packages
import os
import subprocess
import sys
from setuptools.command.install import install
from setuptools.command.develop import develop
from setuptools.command.build_py import build_py

class BuildRustLib(install):
    """自定义安装命令，编译 Rust 库"""
    def run(self):
        try:
            subprocess.check_call(['cargo', 'build', '--release'], cwd='rust_engine')
        except subprocess.CalledProcessError as e:
            print(f"Cargo build failed: {e}")
            raise
        super().run()

class BuildRustLibDevelop(develop):
    """自定义开发安装命令，编译 Rust 库"""
    def run(self):
        try:
            subprocess.check_call(['cargo', 'build'], cwd='rust_engine')
        except subprocess.CalledProcessError as e:
            print(f"Cargo build failed: {e}")
            raise
        super().run()

class BuildPyCommand(build_py):
    """自定义构建命令，确保 Rust 库存在"""
    def run(self):
        try:
            subprocess.check_call(['cargo', 'build', '--release'], cwd='rust_engine')
        except subprocess.CalledProcessError as e:
            print(f"Cargo build failed: {e}")
            raise
        super().run()

setup(
    name="risk_contagion_analysis",
    version="0.1.0",
    author="Your Name",
    author_email="your.email@example.com",
    description="A risk contagion analysis system for supply chains",
    long_description=open("README.md").read(),
    long_description_content_type="text/markdown",
    url="https://github.com/yourusername/risk_contagion_analysis",
    packages=find_packages(),
    package_data={
        "risk_contagion_analysis": ["rust_engine/target/release/*.so", "rust_engine/target/release/*.dll", "rust_engine/target/release/*.dylib"],
    },
    include_package_data=True,
    classifiers=[
        "Programming Language :: Python :: 3",
        "Programming Language :: Rust",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        "Topic :: Scientific/Engineering :: Information Analysis",
    ],
    python_requires=">=3.6",
    install_requires=[
        "numpy>=1.19.0",
        "pandas>=1.0.0",
        "networkx>=2.5",
        "matplotlib>=3.3.0",
        "scikit-learn>=0.23.0",
        "pytest>=6.0.0",
    ],
    cmdclass={
        'install': BuildRustLib,
        'develop': BuildRustLibDevelop,
        'build_py': BuildPyCommand,
    },
)
